//
//  RCDPageViewController.m
//  SchoolHelper
//
//  Created by 任辰东 on 2018/3/22.
//  Copyright © 2018年 JSY. All rights reserved.
//

#import "RCDPageViewController.h"
#import "NSString+Size.h"

@interface RCDPageViewController ()

@property (nonatomic, strong) NSArray *viewControllers;

@end

@implementation RCDPageViewController

- (instancetype)initWithViewControllers:(NSArray<RCDBaseViewController *> *)controllers andTheirTitles:(NSArray<NSString *> *)titles
{
    if (self = [super init]) {
        NSParameterAssert(controllers.count == titles.count);
        self.viewControllers = [NSArray arrayWithArray:controllers];
        self.titles = [NSArray arrayWithArray:titles];
        
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
        //忽略此处的警告，一定能在父类里找到setup方法
        if ([self respondsToSelector:@selector(wm_setup)]) {
            [self performSelector:@selector(wm_setup) withObject:nil];//老版本
        }
        //        if ([self respondsToSelector:@selector(wm_setup)]) {
        //            [self performSelector:@selector(wm_setup) withObject:nil];//老版本
        //        }
        //        self.showOnNavigationBar = YES;
        //        [self performSelector:@selector(setup) withObject:nil];
        
        
#pragma clang diagnostic pop
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.menuView.backgroundColor = self.menuViewBackgroudColor;
    //    self.showOnNavigationBar = YES;
    //    [self setCustomStyle];
}

- (void)setCustomStyle
{
    self.showOnNavigationBar = NO;
    self.pageAnimatable = YES;
//    self.menuBGColor = [UIColor whiteColor];
    self.progressColor = UIColorFromHex(kColorMain);
    self.titleColorNormal = UIColorFromHex(0x666666);
    self.titleColorSelected = UIColorFromHex(kColorMain);
//    self.menuItemWidth = 40;
//    self.menuHeight = 50;
    self.menuViewBackgroudColor = [UIColor whiteColor];
    self.menuViewHeight = 50;
    self.postNotification = YES;
    self.bounces = YES;
    self.menuViewStyle = WMMenuViewStyleLine;
    self.titleSizeSelected = 17;
    self.titleSizeNormal = 17;

    //不加此行代码menu的下划线无法显示。。。
    self.view.backgroundColor = UIColorFromHex(kColorBG);
    self.scrollView.backgroundColor = UIColorFromHex(kColorBG);

    [self.menuView addBottomLine];
}

#pragma mark - Data source
- (NSInteger)childControllersCount {
    if ([self.dataSource respondsToSelector:@selector(numbersOfChildControllersInPageController:)]) {
        return [self.dataSource numbersOfChildControllersInPageController:self];
    }
    return self.viewControllers.count;
}

- (UIViewController *)initializeViewControllerAtIndex:(NSInteger)index {
    if ([self.dataSource respondsToSelector:@selector(pageController:viewControllerAtIndex:)]) {
        return [self.dataSource pageController:self viewControllerAtIndex:index];
    }
    return self.viewControllers[index];
}

- (NSString *)titleAtIndex:(NSInteger)index {
    if ([self.dataSource respondsToSelector:@selector(pageController:titleAtIndex:)]) {
        return [self.dataSource pageController:self titleAtIndex:index];
    }
    return self.titles[index];
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
//    CGFloat width = [super menuView:menu widthForItemAtIndex:index];
//    return width + 20;
    
//    NSString *title = self.titles[index];
//    return [title widthWithFont:[UIFont systemFontOfSize:20] constrainedToHeight:30];
    return kScreenWidth/self.viewControllers.count;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    
    CGFloat height = self.menuViewHeight == 0? 49:self.menuViewHeight;
    // CGFloat originY = self.showOnNavigationBar ? 0 : CGRectGetMaxY(self.navigationController.navigationBar.frame);
    CGFloat originY = 0;
    return CGRectMake(0, originY, self.view.frame.size.width, height);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, self.view.frame.size.height - originY);
}

@end
